package com.sicheng.datastruct.hash;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class TowSum {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[]{1,2,4,5,6,3}, 4)));
    }
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        ArrayList<Integer> index = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(target-nums[i]))
                map.put(nums[i],i);
            else{
                index.add(map.get(target-nums[i]));
                index.add(i);
            }

        }
        return index.stream().mapToInt(Integer::valueOf).toArray();
    }
}
